package cn.xinfei.xdecision.data.core.util;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class JsonUtils {

    public static JSONObject readJsonFile(InputStream jsonStream,String fileName){
        String jsonString = "";
        try {
            Reader reader = new InputStreamReader(jsonStream, StandardCharsets.UTF_8);
            int ch;
            StringBuffer stringBuffer = new StringBuffer();
            while ((ch = reader.read()) != -1){
                stringBuffer.append((char) ch);
            }
            reader.close();
            jsonString = stringBuffer.toString();
        } catch (FileNotFoundException e){
            JSONObject notFoundJson = new JSONObject();
            notFoundJson.put("msg",fileName + " 文件不存在！");
            return notFoundJson;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonString);
    }
}
